//
//  LAMerchantDetailHeaderView.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/10/21.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAMerchantDetailHeaderView.h"
#import "LZRestaurantDetailModel.h"
#import "LZSellerPhotoAlbumsModel.h"
#import "LABaseKeyValueLabelView.h"
#import "HCSStarRatingView.h"
#import "KDCycleBannerView.h"
#import "Masonry.h"

@interface LAMerchantDetailHeaderView()<KDCycleBannerViewDataSource, KDCycleBannerViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *viewAD;    // 广告栏（轮播图）
@property (strong, nonatomic) KDCycleBannerView *cycleView;
@property (weak, nonatomic) IBOutlet UILabel *labTitle;     // 标题
@property (weak, nonatomic) IBOutlet UIView *starBgView;  // 评价指数
@property (weak, nonatomic) IBOutlet UILabel *labNoReviewRemind;
@property (weak, nonatomic) IBOutlet UILabel *labPrice;     // 人均消费
@property (weak, nonatomic) IBOutlet UILabel *labReview;    // 评价数
@property (weak, nonatomic) IBOutlet UILabel *labStyleCook; // 菜系
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labTitleToTopConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *starBgViewWidthConstraint;

@property (weak, nonatomic) IBOutlet LABaseKeyValueLabelView *tasteView; // 味道评分
@property (weak, nonatomic) IBOutlet LABaseKeyValueLabelView *atmosphereView; // 气氛评分
@property (weak, nonatomic) IBOutlet LABaseKeyValueLabelView *serviceView; // 服务评分

@property (weak, nonatomic) HCSStarRatingView *starRatingView;

@end

@implementation LAMerchantDetailHeaderView

+(LAMerchantDetailHeaderView *)instanceLAMerchantDetailHeaderView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"LAMerchantDetailHeaderView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.labTitle.preferredMaxLayoutWidth = SCREEN_WIDTH - 30 - 80;
    self.labNoReviewRemind.text = LZLocalizedString(@"restaurant_norating_now", nil);
    self.starBgView.backgroundColor = [UIColor clearColor];
    self.labPrice.layer.cornerRadius = 2.0;
    self.labPrice.layer.borderWidth = 0.5;
    self.labPrice.layer.borderColor = UIColorFromRGB(0xcccccc, 1).CGColor;
    
    /*
     ==========================================Star =================================================
     */
    
    int number = 5;
    HCSStarRatingView *star = [[HCSStarRatingView alloc]initWithFrame:CGRectZero];
    star.maximumValue = number;
    star.minimumValue = 0;
    star.spacing = 2.5;
    star.allowsHalfStars = YES;
    star.accurateHalfStars = YES;
    star.tintColor = UIColorFromRGB(0xF08F4D, 1);
    star.userInteractionEnabled = NO;
   
    self.starRatingView = star;
    
    [self.starBgView addSubview:star];
    
    [star makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.starBgView);
    }];
}

- (KDCycleBannerView*)cycleView {
    if (_cycleView) {
        return _cycleView;
    }
    
    _cycleView = [[KDCycleBannerView alloc] init];
    _cycleView.datasource = self;
    _cycleView.delegate = self;
    _cycleView.continuous = YES;
    _cycleView.autoPlayTimeInterval = 5;
    return _cycleView;

}

-(void)setModel:(LZRestaurantDetailModel *)model {
    _model = model;
    if (self.haveADView) {
        [self.viewAD addSubview:self.cycleView];
        [self.cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.viewAD);
        }];
    }
    else {
        [self.viewAD removeFromSuperview];
        self.labTitleToTopConstraint.constant = 20;
    }
    
    // 店铺名
    self.labTitle.text = model.restaurantName;
    // 菜系
    self.labStyleCook.text = model.cookingStyle;
    // 人均消费
    self.labPrice.text = [NSString stringWithFormat:@" %.3f%@ ", model.averageConsumption,LZLocalizedString(@"restaurant_money_person", nil)];

    // 评论数
    self.labReview.text = [NSString stringWithFormat:@"%ld",model.remarkCountPerson];
    
    BOOL isHaveReview = model.averageScore>0;

    // 评论口味分数
    [self.tasteView setKey:[NSString stringWithFormat:@"%@:",LZLocalizedString(@"taste", nil)] andValue:(isHaveReview?[NSString stringWithFormat:@"%.1f",(float)model.remarkCountTaste/model.remarkCountPerson]:@"0.0")];
    // 评论环境分数
    [self.atmosphereView setKey:[NSString stringWithFormat:@"%@:",LZLocalizedString(@"atmosphere", nil)] andValue:(isHaveReview?[NSString stringWithFormat:@"%.1f",(float)model.remarkCountEnv/model.remarkCountPerson]:@"0.0")];
    // 评论服务分数
    [self.serviceView setKey:[NSString stringWithFormat:@"%@:",LZLocalizedString(@"service", nil)] andValue:(isHaveReview?[NSString stringWithFormat:@"%.1f",(float)model.remarkCountService/model.remarkCountPerson]:@"0.0")];
    
    // 星星指数
    self.starRatingView.hidden = !isHaveReview;
    self.labNoReviewRemind.hidden = isHaveReview;
    if (isHaveReview) {
        self.starRatingView.value = [model.averageScore floatValue];
        self.starBgViewWidthConstraint.constant = 80;
    }
    else {
        self.starBgViewWidthConstraint.constant = [self.labNoReviewRemind intrinsicContentSize].width;
    }

}

-(CGFloat)judgeHeaderViewHeight {
    
    CGFloat topImage_h = (self.haveADView?SCREEN_WIDTH*2./3.:0);   // 头图的高度
    
    CGFloat padding1 = 15;  // 头图到标题的距离
    
    CGFloat labTitle_h = self.labTitle.intrinsicContentSize.height; // 标题的高度
    
    CGFloat labTitleOrImageStar_h = (labTitle_h>12)?labTitle_h:12; // 标题或星星，取最大值
    
    CGFloat padding2 = 7;  // 标题（或星星，取最大值）菜系的距离
    
    CGFloat labStyleCook_h = self.labStyleCook.intrinsicContentSize.height;    // 菜系高度

    CGFloat padding3 = 15;  // 菜系到价格和评论那一栏的距离

    CGFloat imgIcon = 17;   // 价格和评论那一栏的高度
    
    CGFloat padding4 = 15;  // 价格和评论那一栏到线的距离（线的高度忽略）
    
    CGFloat padding5 = 15;  // 线到下边评分的的距离
    
    CGFloat labTaste_h = [self getTextHeightWithText:@"Taste:" Font:[UIFont systemFontOfSize:14] Width:SCREEN_WIDTH]; // 评分一栏的高度
    
    CGFloat padding6 = 23;  // 评分一栏到底部的距离
    
    CGFloat totle = topImage_h + padding1 + labTitleOrImageStar_h + padding2 + imgIcon + padding3 + padding4 + labStyleCook_h + padding5 + labTaste_h + padding6;
    
    return totle;
}

- (CGFloat)getTextHeightWithText:(NSString *)text Font:(UIFont *)font Width:(CGFloat)width {
    
    NSDictionary *attributes = @{
                                 NSFontAttributeName: font,
                                 };
    CGRect expectedLabelRect = [text boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:attributes
                                                  context:nil];
    return CGRectGetHeight(expectedLabelRect);
}

#pragma mark - KDCycleBannerViewDataSource

- (NSArray *)numberOfKDCycleBannerView:(KDCycleBannerView *)bannerView {
    
    NSMutableArray *arrImgUrls = [NSMutableArray array];
    for (int i = 0; i<self.model.sellerPhotoAlbums.count; i++) {
        LZSellerPhotoAlbumsModel *sellerPhotoAlbumsModel = self.model.sellerPhotoAlbums[i];
        NSString *strUrl = [NSString stringWithFormat:@"%@%@",APIBaseImageUrl,sellerPhotoAlbumsModel.photoUrl];
        [arrImgUrls addObject:strUrl];
    }
    
    return arrImgUrls;
}

- (UIViewContentMode)contentModeForImageIndex:(NSUInteger)index {
    return UIViewContentModeScaleToFill;
}

- (UIImage *)placeHolderImageOfBannerView:(KDCycleBannerView *)bannerView atIndex:(NSUInteger)index
{
    return [UIImage imageNamed:@"lz_cycle_placehoiderImage"];
}

- (UIImage *)placeHolderImageOfZeroBannerView {
    return [UIImage imageNamed:@"lz_cycle_placehoiderImage"];
}

#pragma mark - KDCycleBannerViewDelegate

- (void)cycleBannerView:(KDCycleBannerView *)bannerView didScrollToIndex:(NSUInteger)index {
    // NSLog(@"didScrollToIndex:%ld", (long)index);
}

- (void)cycleBannerView:(KDCycleBannerView *)bannerView didSelectedAtIndex:(NSUInteger)index {
//        NSLog(@"didSelectedAtIndex:%ld", (long)index);
//    XbnHomeFocusModel * model = self.focusArrays[index];
//    if (model && model.focusUrl) {
//        if([model.focusUrl length] != 0) {
//            XbnActivityDetailsViewController * activityDetailVC = [[XbnActivityDetailsViewController alloc]
//                                                                   initWithUrl:model.focusUrl];
//            [self.baseVC.navController pushViewController:activityDetailVC animated:YES];
//        }
//    }
}

@end
